﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CameraMoved : MonoBehaviour {

	private Camera camera;
	public float cameraMoveSpeed = 0.01f;
	public GameObject target;
	public List<GameObject> followList;
	private Vector3 oldPosition;
	private int saveScore;
	private bool isFirst;
	public float cameraMaxSpeed;
	void Start () {
		saveScore = 0;
		isFirst = true;
		camera = GetComponent<Camera> ();
		oldPosition = transform.position;
		AudioManager.instance.PlayBGaudio (Random.value > 0.5 ? "bg2" : "bg3");
		if(PlayerPrefs.GetInt("select") == 1){
			if (PlayerPrefs.GetInt ("hasCamera") == 1) {
				cameraMoveSpeed = PlayerPrefs.GetFloat ("cameraIF");
			}
			if (PlayerPrefs.GetInt ("hasCameraMaxSpeed") == 1) {
				cameraMaxSpeed = PlayerPrefs.GetFloat ("cameraMaxSpeed");
			}
		}
	}
		
	void Update () {
		oldPosition = transform.position;
		if (target != null && camera.WorldToScreenPoint (target.transform.position).x >= Screen.width / 4f * 3f) {
			var newSpeed = Mathf.SmoothStep (camera.transform.position.x, target.transform.position.x, Time.deltaTime*10) - camera.transform.position.x;
			if (newSpeed < cameraMoveSpeed * Time.deltaTime) {
				newSpeed = cameraMoveSpeed * Time.deltaTime;
			}
	 		camera.transform.position = new Vector3 (camera.transform.position.x+ newSpeed, camera.transform.position.y, camera.transform.position.z);
		} else {
			camera.transform.position = new Vector3 (camera.transform.position.x + cameraMoveSpeed*Time.deltaTime, camera.transform.position.y, camera.transform.position.z);
		} 
		if (followList.Count > 0) {
			float oX = camera.transform.position.x - oldPosition.x;
			for (var i = 0; i < followList.Count; i++) {
				var obj = followList [i];
				obj.transform.position = new Vector3 (obj.transform.position.x + oX, obj.transform.position.y, obj.transform.position.z);
			}
		}
		UpdateSpeed ();
	}

	public void UpdateSpeed(){
		var scoreCouter = GameObject.Find ("Canvas").GetComponent<ScoreCounter> ();
		if (isFirst) {
			if (scoreCouter.currentScore - saveScore >= 20) {
				cameraMoveSpeed *= 1.2f;
				saveScore = scoreCouter.currentScore;
			}
			isFirst = false;
		} else {
			if (scoreCouter.currentScore - saveScore >= 10) {
				cameraMoveSpeed *= 1.2f;
				saveScore = scoreCouter.currentScore;
			}
		}
		if (cameraMoveSpeed >= cameraMaxSpeed) {
			cameraMoveSpeed = cameraMaxSpeed;
		}

	}

	public bool OutLeft(float X){
		if (camera.WorldToScreenPoint (new Vector3 (X, 0, 0)).x < 0) {
			return true;
		}
		return false;
	}

	public Vector2 getSizeWorldSpace(SpriteRenderer sprite){
		Vector2 sprite_size = sprite.sprite.rect.size;
		Vector2 local_sprite_size = sprite_size / sprite.sprite.pixelsPerUnit;
		Vector3 world_size = local_sprite_size;
		world_size.x *= sprite.transform.lossyScale.x;
		world_size.y *= sprite.transform.lossyScale.y;

		return world_size;
	}

	public Vector2 getSizeScreenSpace(SpriteRenderer sprite){
		Vector2 sprite_size = sprite.sprite.rect.size;
		Vector2 local_sprite_size = sprite_size / sprite.sprite.pixelsPerUnit;
		Vector3 world_size = local_sprite_size;

		Vector3 screen_size = 0.5f * world_size / Camera.main.orthographicSize;
		screen_size.y *= Camera.main.aspect;

		return screen_size;
	}

	public Vector3 getSizePixel(SpriteRenderer sprite){
		Vector2 sprite_size = sprite.sprite.rect.size;
		Vector2 local_sprite_size = sprite_size / sprite.sprite.pixelsPerUnit;
		Vector3 world_size = local_sprite_size;
		Vector3 screen_size = 0.5f * world_size / Camera.main.orthographicSize;
		screen_size.y *= Camera.main.aspect;
		Vector3 in_pixels = new Vector3(screen_size.x * Camera.main.pixelWidth, screen_size.y * Camera.main.pixelHeight, 0) * 0.5f;
		return in_pixels;
	}
}
